package servlet;

import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import grade.Grade;
import grade.GradeDao;
import student.*;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		String stuno = request.getParameter("stuno");
		String pass = request.getParameter("pass");
		StudentDao sdao = new StudentDao();
		NewsUser nu = sdao.login(Long.parseLong(stuno), pass);
		if (nu != null) {
//			Cookie c1 = new Cookie("STUNO", stuno);
//			c1.setMaxAge(3600 * 24 * 7);
//			Cookie c2 = new Cookie("PASS", pass);
//			c2.setMaxAge(3600 * 24 * 7);
//			response.addCookie(c1);
//			response.addCookie(c2);
			request.getSession().setAttribute("newsUser", nu);
			request.getRequestDispatcher("SelectServlet").forward(request, response);
			return;
		}
		out.println("<script>alert('用户学号或密码错误');history.back();</script>");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
